package butines.core.game;

import butines.core.math.Vector2f;

public abstract class EntityMoving extends AbstractGameEntity implements Vehicle {

	private float maxForce = .15f;
	private float maxSpeed = 1f;
	private float mass     = 1f;
	private float speed;
	
	private Vector2f velocity = new Vector2f();
	private Vector2f accel    = new Vector2f();

	public EntityMoving() {
	}

	public void setSpeed(float speed) {
		if (speed == 0) {
			this.speed = 0;
			this.velocity.set(0, 0);
		} else if (speed > 0) {
			this.speed = speed;
			this.velocity.mult(getForward(), Math.min(speed, maxSpeed));
		} else {
			throw new IllegalArgumentException("speed is negative");
		}
	}

	public float getSpeed() {
		return speed;
	}

	
	public void applySteeringForce(Vector2f steeringForce) {
		
		accel.set(steeringForce);
		Vector2f.truncate(accel, maxForce);
		
		velocity.add(accel);
		Vector2f.truncate(velocity, maxSpeed);
		

		getPosition().add(velocity);

		speed = velocity.length();
		
		if (speed > 0) {
			getForward().normalise(velocity);
			getSide().set(-getForward().y, getForward().x);
		}
		
	}

	public void applyBrakingForce(float rate) {

		velocity.mult(rate);
		
		speed = velocity.length();
	}

    public Vector2f getPredictFuturePosition() {
    	Vector2f predictFuturePosition = new Vector2f();
    	predictFuturePosition.add(getPosition(), getVelocity());
		return predictFuturePosition;
	}

	public float getMass() {
		return mass;
	}

	public void setMass(float mass) {
		this.mass = mass;
	}

	public float getMaxForce() {
		return maxForce;
	}

	public void setMaxForce(float maxForce) {
		this.maxForce = maxForce;
	}

	public float getMaxSpeed() {
		return maxSpeed;
	}

	public void setMaxSpeed(float maxSpeed) {
		this.maxSpeed = maxSpeed;
	}

	public Vector2f getVelocity() {
		return velocity;
	}

	public Vector2f getAccel() {
		return accel;
	}

}
